package com.shiyuan.bisheng.controller;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.UUID;

@RestController
@RequestMapping(value = "/file")
@Slf4j
public class FileController {

    private static String webUrlFormat = "http://%s/apps/editor/openEditor?data=%s";

    @GetMapping(path = "/file/info/{fileId}")
    public String getWebFileUrl(@PathVariable("fileId") String fileId){

        BiShengFileData biShengFileData = new BiShengFileData();
        FileDocBO fileDocBO = new FileDocBO();
        FileUser fileUser = new FileUser();
        biShengFileData.setDoc(fileDocBO);
        biShengFileData.setUser(fileUser);
        String result = JSON.toJSONString(biShengFileData);
        System.out.println(result);
        String encodeString = Base64.getEncoder().encodeToString(result.getBytes());
        System.out.println(encodeString);
        return String.format(webUrlFormat, "106.15.193.39:8083", encodeString);
    }


    @PostMapping(path = "/callback/save")
    public String fileSaveCallBack(@RequestBody FileCallBackBO fileCallBackBO) throws IOException {
        System.out.println(JSON.toJSONString(fileCallBackBO));
        log.info("params:   " + JSON.toJSONString(fileCallBackBO));
        // 文件下载路径
        // docUrl 文件下载路径
        String downLoadUrl = fileCallBackBO.getData().getDocURL();
        HttpGet httpGet = new HttpGet(downLoadUrl);
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse execute = httpClient.execute(httpGet);
        File file = new File("test.pptx");
        String s = EntityUtils.toString(execute.getEntity());
        FileCopyUtils.copy(s.getBytes(), file);
        return "dada";
    }

    @Data
    public static class BiShengFileData{
        private FileDocBO doc;
        private FileUser user;
    }

    @Data
    public static class FileDocBO{
        private String docId = UUID.randomUUID().toString().replace("-", "");
        private String title="测试文件.pptx";
        private String fetchUrl = "https://shop-mobile-file.oss-cn-beijing.aliyuncs.com/PPPT.pptx";
    }

    @Data
    public static class FileUser{

        private String uid = "admin";

        private String nickName = "名称名字";

        private String avatar = "https://shop-mobile-file.oss-cn-beijing.aliyuncs.com/user_logo.jpg";
    }

    public static void main(String[] args) {
        new FileController().getWebFileUrl("dada");
    }
}
